# -*- coding: utf-8 -*-
# @Time : 2020/10/31 15:00
# @Author : Houor
# @Site : https://my.oschina.net/houor
# @File : dichotomy.py
# @Description: 二分法

from ods.functions import derivative_tri_equation

# 需要使用导数性质

func = derivative_tri_equation

epsilon = 0.004
flag = False
idx = 1

a = 0
b = 2

while not False and abs(b - a) > epsilon:

    x = (a + b) / 2.0
    df = derivative_tri_equation(x)

    print('idx = ', idx, ':')
    print('\t\t 缩减区间前:')
    print('\t\ta = ', a, ', b = ', b)
    print('\t\tx = ', x, ', df = ', df)

    if df < 0:
        a = x
    elif df > 0:
        b = x
    else:
        flag = True

    print('\t\t 缩减区间后:')
    print('\t\ta = ', a, ', b = ', b)
    print('\t\tx = ', x, ', df = ', df)
    print('\t\t|b-a| = ', b - a)
    idx = idx + 1

print('x = ', x)
